﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 整型时间转换器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSwap_Click(object sender, EventArgs e)
        {
            if (cbInteger2DateTime.Checked) {
                integer2DateTime();
            } else {
                dateTime2Integer();
            }
            
        }

        private void integer2DateTime() {
            DateTime dt = Convert.ToDateTime("2006-01-01 00:00:00");

            try {

                string text = tbInteger.Text.Trim();
                long times = 0;
                if (text.Contains(" ")) {//16进制
                    string[] strs = text.Split(' ');
                    for (int i = strs.Length - 1; i >= 0; i--) {
                        times += Convert.ToInt64(strs[i], 16) << 8 * (strs.Length - 1 - i);
                    }
                } else {//十进制
                    times = Convert.ToInt32(text);
                }

                dt = dt.AddSeconds(times);
                laResult.Text = "结果为：" + dt.ToString("yyyy-MM-dd HH:mm:ss");
            } catch {
                laResult.Text = "转换错误！";
            }
        }

        private void dateTime2Integer() {
            string text = tbInteger.Text.Trim();
            try {
                DateTime dt = Convert.ToDateTime(text);
                DateTime oldDt = Convert.ToDateTime("2006-01-01 00:00:00");
                TimeSpan ts = dt - oldDt;
                long times = (long)ts.TotalSeconds;
                laResult.Text = "结果为：" + times.ToString("X10");
            } catch {
                laResult.Text = "转换错误！";
            }
            
        }

        private void cbInteger2DateTime_CheckedChanged(object sender, EventArgs e)
        {
            cbDateTime2Integer.Checked = !cbInteger2DateTime.Checked;

            //laShow.Visible = cbDateTime2Integer.Checked;

            if (cbInteger2DateTime.Checked) {
                label1.Text = "输入整型:";
                laShow.Text = "整型格式为：XX XX XX XX XX";
            } else {
                label1.Text = "输入日期:";
                laShow.Text = "日期格式为：yyyy-MM-dd HH:mm:ss";
            }
        }

        private void cbDateTime2Integer_CheckedChanged(object sender, EventArgs e)
        {
           // cbDateTime2Integer.Checked = !cbInteger2DateTime.Checked;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cbInteger2DateTime.Checked = true;
        }
    }
}
